/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import Entity.Sach;
import Util.DatabaseHandle;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author copang
 */
public class SachImpl implements SachDAO{

    public boolean add(Sach data) {
        String sql = "{ call sp_SACH_add(?,?,?,?,?,?,?,?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {
            db.setOutParameter(1, java.sql.Types.INTEGER);
            db.setParameter(2, data.getMaDauSach());
            db.setParameter(3, data.getGiaBia());
            db.setParameter(4, data.getNamXB());
            db.setParameter(5, data.getBiaTruoc());
            db.setParameter(6, data.getBiaSau());
            db.setParameter(7, data.getGhiChu());
            db.setParameter(8, data.getSoLuongTon());

            flag = db.executeStorePro();
            if (flag) 
                data.setMaSach(db.getIntOutValue(1));
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean delete(int MaSach) {
        String sql = "{ call sp_SACH_delete(?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);

        try {

            db.setParameter(1, MaSach);
            db.executeStorePro();
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean edit(Sach data) {
        String sql = "{ call sp_SACH_edit(?,?,?,?,?,?,?,?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);

        try {
            db.setParameter(1, data.getMaSach());
            db.setParameter(2, data.getMaDauSach());
            db.setParameter(3, data.getGiaBia());
            db.setParameter(4, data.getNamXB());
            db.setParameter(5, data.getBiaTruoc());
            db.setParameter(6, data.getBiaSau());
            db.setParameter(7, data.getGhiChu());
            db.setParameter(8, data.getSoLuongTon());

            db.executeStorePro();
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public ResultSet getAll() {
        String sql = "{call sp_SACH_getAll()}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        return db.excuteQuery();
    }

    public ResultSet findByTenSach(String name) {
        String sql = "{call sp_SACH_findByTenSach(?)}";        
        DatabaseHandle db = new DatabaseHandle(sql, false);
        
        try {
            db.setParameter(1, name);
            return db.excuteQuery();
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public ResultSet findByNXB(String name) {
        String sql = "{call sp_SACH_findByNXB(?)}";        
        DatabaseHandle db = new DatabaseHandle(sql, false);
        
        try {
            db.setParameter(1, name);
            return db.excuteQuery();
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public ResultSet findByTheLoai(String name) {
        String sql = "{call sp_SACH_findByTheLoai(?)}";        
        DatabaseHandle db = new DatabaseHandle(sql, false);
        
        try {
            db.setParameter(1, name);
            return db.excuteQuery();
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public ResultSet findByTacGia(String name) {
        String sql = "{call sp_SACH_findByTacGia(?)}";        
        DatabaseHandle db = new DatabaseHandle(sql, false);
        
        try {
            db.setParameter(1, name);
            return db.excuteQuery();
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public ResultSet getAllWithTenSach() {
        String sql = "{call sp_SACH_getAllWithTenSach()}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        return db.excuteQuery();
    }
    
    public boolean subSoLuongTon(int maSach, int soLuong) {
        String sql = "{call sp_SACH_subSoLuongTon(?,?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        try {
            db.setParameter(1, maSach);
            db.setParameter(2, soLuong);
            db.executeStorePro();
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
